package com.woniuxy.carlife.carlifecommonsweb.exception;

import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

/**
 * @author zjq
 * @ClassName:CarlifeGlobalException
 * @Description:全局异常处理总类
 * @date 2021/4/6 13:54
 **/

@RestControllerAdvice
public class CarlifeGlobalException extends Throwable {

    /**
     * 功能描述〈空指向异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:55
     */

    @ExceptionHandler(NullPointerException.class)
    public Result nullPointException(NullPointerException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈类不存在异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:55
     */

    @ExceptionHandler(ClassNotFoundException.class)
    public Result classNotFoundException(ClassNotFoundException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈下标越界异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:56
     */

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result indexOutOfBoundsException(IndexOutOfBoundsException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈参数传递异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:56
     */

    @ExceptionHandler(IllegalArgumentException.class)
    public Result illegalArgumentException(IllegalArgumentException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈数学运算异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:56
     */

    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticException(ArithmeticException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈类型转换异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:57
     */

    @ExceptionHandler(ClassCastException.class)
    public Result classCastException(ClassCastException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈找不到指定方法异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:57
     */

    @ExceptionHandler(NoSuchMethodException.class)
    public Result noSuchMethodException(NoSuchMethodException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈实例化异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:57
     */

    @ExceptionHandler(InstantiationException.class)
    public Result instantiationException(InstantiationException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈io异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:57
     */

    @ExceptionHandler(IOException.class)
    public Result ioException(IOException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈数值转化异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:57
     */

    @ExceptionHandler(NumberFormatException.class)
    public Result numberFormatException(NumberFormatException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }





    /*
     * 功能描述〈jwt过期〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 4:20
     * @throws
     */

    @ExceptionHandler(ExpiredJwtException.class)
    public Result ExpiredJwtException(Exception e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

    /**
     * 功能描述〈未知异常〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: org.springframework.http.ResponseEntity<java.lang.String>
     * @Date 2021/4/6 13:58
     */

    @ExceptionHandler(Exception.class)
    public Result Exception(Exception e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }

}
